
var User = require('../models/user')
var crypto = require('crypto')
var Post = require('../models/post')

function checkLogin(req, res, next){
  if(!req.session.user){
    req.flash('error', '未登录')
    res.redirect('/login')
  }
  next()
}
function checkNotLogin(req, res, next){
  if(req.session.user){
    req.flash('error', '已经登录')
    res.redirect('/')
  }
  next()
}

module.exports = function(app){
  //所有的路由操作都会放到这里
  //首页
  app.get('/', function(req, res){
    Post.get(null, function(err, posts){
      if(err){
        posts = []
      }
      res.render('index', {
        title:'首页',
        user:req.session.user,
        posts:posts,
        success:req.flash('success').toString(),
        error:req.flash('error').toString()
      })
    })
  })
  //注册页面
  app.get('/reg',checkNotLogin, function(req ,res){
    res.render('reg',{
      title:'注册页面',
      user:req.session.user,
      success:req.flash('success').toString(),
      error:req.flash('error').toString()
    })
  })
  //注册行为
  app.post('/reg',checkNotLogin, function(req, res){
    var name = req.body.name
    var password = req.body.password
    var password_re = req.body['password-repeat']

    //判断两次密码是否一致
    if(password != password_re){
      req.flash('error','两次输入的密码不一致')
      return res.redirect('/reg')
    }
    //对密码进行加密
    var md5 = crypto.createHash('md5')
    var password = md5.update(req.body.password).digest('hex')
    //把数据存一个新对象
    var newUser = new User({
      name:name,
      password:password,
      email:req.body.email
    })

    User.get(newUser.name, function(err, user){
      if(err){
        req.flash('error',err)
        return res.redirect('/reg')
      }
      //判断用户是否存在
      if(user){
        req.flash('error', '该用户名已经存在')
        return res.redirect('/reg')
      }
      //如果不存在，就新增这个用户
      newUser.save(function(err, user){
        if(err){
          req.flash('error', err)
          return res.redirect('/reg')
        }
        req.session.user = user
        req.flash('success', '注册成功')
        return res.redirect('/')
      })
    })
  })

  //登录页面
  app.get('/login',checkNotLogin,function(req, res){
    res.render('login',{
      title:'登录页面',
      user:req.session.user,
      success:req.flash('success').toString(),
      error:req.flash('error').toString()
    })
  })
  app.post('/login',checkNotLogin, function(req, res){
    var md5 = crypto.createHash('md5')
    var password = md5.update(req.body.password).digest('hex')

    User.get(req.body.name, function (err, user) {
      if(err){
        req.flash('error', '用户名已经存在')
        return res.redirect('/login')
      }
      if(!user){
        req.flash('error', '用户不存在')
       return res.redirect('/login')
      }
      if(user.password != password){
        req.flash('error', '密码错误')
        return res.redirect('/login')
      }
      req.session.user = user
      req.flash('success', '登录成功')
      return res.redirect('/')
    })
  })

  //退出
  app.get('/logout',checkLogin,function(req ,res){
    req.session.user = null
    req.flash('success', '退出成功')
    res.redirect('/')
  })

  //发表文章
  app.get('/post',checkLogin, function(req ,res){
    res.render('post',{
      title:'发表',
      user:req.session.user,
      success:req.flash('success').toString(),
      error:req.flash('error').toString()
    })
  })
  app.post('/post',checkLogin, function(req ,res){
    //1.数据库收集
    //2.数据保存
    //3.跳转到首页
    var currentUser = req.session.user.name
    var post = new Post(currentUser, req.body.title, req.body.post,req.ip)
    post.save(function(err){
      if(err){
        req.flash('error', err)
        res.redirect('/')
      }
      req.flash('success', '发表成功')
      res.redirect('/')
    })
  })
}